# TODO(nadir): Eliminate dependency on installed nixysa by checking out the
#   code from the web automatically during build if not in existance.

import os
import sys

IDL_SOURCES=['browntooth_npapi.idl']
SOURCES=['plugin.cc', 'browntooth_npapi.cc', 'send_receive.cc']
STATIC_GLUE_SOURCES=['common.cc', 'npn_api.cc', 'static_object.cc', 'main.cc']

env = Environment(
  NIXYSA_DIR = "$nixysa_dir/nixysa",
  STATIC_GLUE_DIR = '$NIXYSA_DIR/static_glue/npapi',
  NPAPI_DIR = '$nixysa_dir/third_party/npapi/include',
  GLUE_DIR = 'glue',
  CPPPATH = ['.', '$STATIC_GLUE_DIR', '$NPAPI_DIR', '$GLUE_DIR']
)
vars = Variables()
vars.Add("nixysa_dir", "The base directory of the nixysa tree",
         default="../..")
vars.Update(env)

env.Append(ENV={'PYTHON': sys.executable})
if sys.platform == 'win32':
  env.Append(CODEGEN = 'codegen.bat',
             CPPDEFINES = ['WIN32', 'OS_WINDOWS'])
elif sys.platform == 'darwin':
  env.Append(CODEGEN = 'codegen.sh',
             CPPDEFINES = ['XP_MACOSX', 'OS_MACOSX'],
             LINKFLAGS = '-framework CoreFoundation -framework IOKit')
else:
  env.Append(CODEGEN = 'codegen.sh',
             CPPDEFINES = ['OS_LINUX'],
             LINKFLAGS = '-lbluetooth')

def NixysaEmitter(target, source, env):
  bases = [os.path.splitext(s.name)[0] for s in source] + ['globals']
  targets = ['$GLUE_DIR/%s_glue.cc' % b for b in bases]
  targets += ['$GLUE_DIR/%s_glue.h' % b for b in bases]
  targets += ['$GLUE_DIR/hash', '$GLUE_DIR/parsetab.py']
  return targets, source

NIXYSA_CMDLINE = ' '.join([env.File('$NIXYSA_DIR/$CODEGEN').abspath,
                           '--output-dir=$GLUE_DIR',
                           '--generate=npapi',
                           '$SOURCES'])

env['BUILDERS']['Nixysa'] = Builder(action=NIXYSA_CMDLINE,
                                    emitter=NixysaEmitter)

AUTOGEN_OUTPUT = env.Nixysa(IDL_SOURCES)
AUTOGEN_CC_FILES = [f for f in AUTOGEN_OUTPUT if f.suffix == '.cc']

env.SharedLibrary('browntooth_npapi', AUTOGEN_CC_FILES + SOURCES +
                  ['$STATIC_GLUE_DIR/' + f for f in STATIC_GLUE_SOURCES])
